package com.learning.optimize.jdk.regex;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormatParser {

    private static final Pattern PURE_TEXT_PATTERN =
            Pattern.compile("^[^\\[\\]\\s]+(\\s+[^\\[\\]\\s]+)*$");

    private static final Pattern PURE_SYMBOL_PATTERN =
            Pattern.compile("^(\\[[^\\[\\]]+\\])+$");

    private static final Pattern MIXED_PATTERN =
            Pattern.compile("^(?=.*[^\\[\\]\\s])(?=.*\\[[^\\[\\]]+\\]).+$");

    private static final Pattern ELEMENT_PATTERN =
            Pattern.compile("([^\\[\\]]+)|(\\[[^\\[\\]]+\\])");

    public static FormatType detectFormat(String input) {
        if (input == null || input.trim().isEmpty()) {
            return FormatType.UNKNOWN;
        }

        if (PURE_TEXT_PATTERN.matcher(input).matches()) {
            return FormatType.PURE_TEXT;
        } else if (PURE_SYMBOL_PATTERN.matcher(input).matches()) {
            return FormatType.PURE_SYMBOL;
        } else if (MIXED_PATTERN.matcher(input).matches()) {
            return FormatType.MIXED;
        } else {
            return FormatType.UNKNOWN;
        }
    }

    public static List<FormatElement> parseElements(String input) {
        List<FormatElement> elements = new ArrayList<>();

        if (input == null || input.trim().isEmpty()) {
            return elements;
        }

        Matcher matcher = ELEMENT_PATTERN.matcher(input);
        while (matcher.find()) {
            String content = matcher.group();
            ElementType type = content.startsWith("[") && content.endsWith("]")
                    ? ElementType.SYMBOL
                    : ElementType.TEXT;

            // 对于符号，去掉方括号；对于文本，去掉首尾空格
            String value = type == ElementType.SYMBOL
                    ? content.substring(1, content.length() - 1)
                    : content.trim();

            elements.add(new FormatElement(type, value, content));
        }

        return elements;
    }

    public static class FormatElement {
        private final ElementType type;
        private final String value;
        private final String original;

        public FormatElement(ElementType type, String value, String original) {
            this.type = type;
            this.value = value;
            this.original = original;
        }

        // getters
        public ElementType getType() {
            return type;
        }

        public String getValue() {
            return value;
        }

        public String getOriginal() {
            return original;
        }

        @Override
        public String toString() {
            return String.format("%s{value='%s', original='%s'}", type, value, original);
        }
    }

    public enum ElementType {
        TEXT, SYMBOL
    }

    public enum FormatType {
        PURE_TEXT("纯文字"),
        PURE_SYMBOL("纯符号"),
        MIXED("混合格式"),
        UNKNOWN("未知格式");

        private final String description;

        FormatType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    // 使用示例
    public static void main(String[] args) {
        String[] examples = {
                "银行缴费金额，男50岁缴费2年",
                "[a][b][c]",
                "[dlz_sex][dlz_jfqj][dlz_age]",
                "[yywd_city][yywd_area]的[YYWD]",
                "[yywd_province][yywd_city]的[YYWD](地址|在哪|在哪儿|在哪里|在什么地方|位置|位置在哪|地址在哪|信息|具体地址|具体位置)",
                "文本1[a]文本2[b]",
                "[zhiyecc12]的职业代码",
                "实时交费能用[bank]的账户吗",
                "查一下[zycxcctp]职业分类情况",
                "姓名[name]年龄[age]地址[address]"
        };

        for (String example : examples) {
            System.out.println("输入: " + example);
            FormatType type = detectFormat(example);
            System.out.println("格式类型: " + type.getDescription());

            if (type != FormatType.UNKNOWN) {
                List<FormatElement> elements = parseElements(example);
                System.out.println("解析结果:");
                for (FormatElement element : elements) {
                    System.out.println("  " + element);
                }
            }
            System.out.println("---");
        }
    }
}